//
//  MessageFrame.m
//  聊天界面
//
//  Created by 周述坚 on 16/11/12.
//  Copyright © 2016年 周述坚. All rights reserved.
//

#import "JPMessageFrame.h"

@implementation JPMessageFrame

- (void)setMsgM:(JPMessageModel *)msgM {
    _msgM = msgM;
    
    // 设置间距
    CGFloat jianJu = 10;
    
    // 计算timeLabel的Frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = screenW;
    CGFloat timeH = 20;
    if (!msgM.isHiddenTime) {
        _timeF = CGRectMake(timeX, timeY, timeW, timeH);
    }
    // 计算icon的frame
    CGFloat iconW = 35;
    CGFloat iconH = iconW;
    CGFloat iconY = timeY + timeH + jianJu;
    CGFloat iconX;
    if (msgM.type == 0) {
        iconX = screenW - iconW - jianJu;
    } else {
        iconX = jianJu;
    }
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 计算text的frame
    CGSize textSize = [msgM.text boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:TEXT_FONT } context:nil].size;
    
    CGFloat textW = textSize.width + 40;
    CGFloat textH = textSize.height + 40;
    CGFloat textY = iconY;
    CGFloat textX;
    if(msgM.type == 0) {
        textX = iconX - textW;
    } else {
        textX = jianJu + iconW;
    }
    _textF = CGRectMake(textX, textY, textW, textH);
    
    // 有可能会出现文本输出框没有头像框高的情况,这种情况就需要判断,取较大的框
    CGFloat iconMaxY = CGRectGetMaxY(_iconF);
    CGFloat textMaxY = CGRectGetMaxY(_textF);
    
    _rowH = iconMaxY > textMaxY ? iconMaxY : textMaxY;
}

@end
